<template>
  <div class="app-container btn">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="120px"
    >
      <el-form-item label="浏览量" prop="pageView">
        <el-input
          type="number"
          v-model="queryParams.pageView"
          placeholder="请输入浏览量"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="访客数" prop="numberVisitors">
        <el-input
          type="number"
          v-model="queryParams.numberVisitors"
          placeholder="请输入访客数"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="ip数" prop="numberIp">
        <el-input
          type="number"
          v-model="queryParams.numberIp"
          placeholder="请输入ip数"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="跳出率" prop="heartRate">
        <el-input
          type="number"
          v-model="queryParams.heartRate"
          placeholder="请输入跳出率"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- 新平均访问时长 -->
      <el-form-item label="平均访问时长" prop="averageAccessTime">
        <el-time-picker
          clearable
          value-format="HH:mm:ss"
          type="date"
          size="small"
          v-model="form.averageAccessTime"
          placeholder="选择平均访问时长"
        >
        </el-time-picker>
      </el-form-item>
      <el-form-item label="平均访问页数" prop="averageAccessPage">
        <el-input
          type="number"
          v-model="queryParams.averageAccessPage"
          placeholder="请输入平均访问页数"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="发布时间">
        <el-date-picker
          size="small"
          style="width: 220px"
          v-model="Time"
          type="daterange"
          range-separator="至"
          value-format="yyyy-M-d"
          start-placeholder="起始时间"
          end-placeholder="结束时间"
        >
        </el-date-picker>
      </el-form-item>
      <br />
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['trend:trendAnalyses:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['trend:trendAnalyses:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['trend:trendAnalyses:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['trend:trendAnalyses:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      :header-cell-style="{ 'font-size': '17px' }"
      style="width: 100%; font-size: 17px"
      v-loading="loading"
      :data="trendAnalysesList"
      @selection-change="handleSelectionChange"
      stripe
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="趋势分析表id" align="center" prop="id" />
      <el-table-column label="浏览量" align="center" prop="pageView" />
      <el-table-column label="访客数" align="center" prop="numberVisitors" />
      <el-table-column label="ip数" align="center" prop="numberIp" />
      <el-table-column label="跳出率 (%)" align="center" prop="heartRate" />
      <el-table-column
        label="平均访问时长"
        align="center"
        prop="averageAccessTime"
        width="180"
      >
        <template slot-scope="scope">
          <div>{{ scope.row.averageAccessTime }}</div>
        </template>
      </el-table-column>
      <el-table-column
        width="180"
        label="平均访问页数"
        align="center"
        prop="averageAccessPage"
      />

      <el-table-column
        label="发布时间"
        align="center"
        prop="createTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{
            parseTime(scope.row.createTime, "{y}-{m}-{d} {h}:{s}:{m}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['trend:trendAnalyses:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['trend:trendAnalyses:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      class="pagination_style"
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改趋势分析信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form
        ref="form"
        :model="form"
        size="small"
        :rules="rules"
        label-width="140px"
      >
        <el-form-item label="浏览量" prop="pageView">
          <el-input
            type="number"
            v-model="form.pageView"
            placeholder="请输入浏览量"
          />
        </el-form-item>
        <el-form-item label="访客数" prop="numberVisitors">
          <el-input
            type="number"
            v-model="form.numberVisitors"
            placeholder="请输入访客数"
          />
        </el-form-item>
        <el-form-item label="ip数" prop="numberIp">
          <el-input
            type="number"
            v-model="form.numberIp"
            placeholder="请输入ip数"
          />
        </el-form-item>
        <el-form-item label="跳出率" prop="heartRate">
          <el-input
            type="number"
            oninput="if(value>100)value=100;if(value<0)value=null"
            v-model="form.heartRate"
            placeholder="请输入跳出率"
          />
        </el-form-item>
        <!-- 新平均访问时长 -->
        <el-form-item label="平均访问时长" prop="averageAccessTime">
          <el-time-picker
            clearable
            value-format="HH:mm:ss"
            type="date"
            size="small"
            v-model="form.averageAccessTime"
            placeholder="选择平均访问时长"
          >
          </el-time-picker>
        </el-form-item>
        <el-form-item label="平均访问页数" prop="averageAccessPage">
          <el-input
            type="number"
            v-model="form.averageAccessPage"
            placeholder="请输入平均访问页数"
          />
        </el-form-item>

        <el-form-item label="发布时间" prop="createTime">
          <div class="block">
            <el-date-picker
              format="yyyy-MM-dd HH:mm:ss"
              value-format="yyyy-MM-dd HH:mm:ss"
              clearable
              size="small"
              v-model="form.createTime"
              type="datetime"
              placeholder="选择日期时间"
              default-time="12:00:00"
            >
            </el-date-picker>
          </div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listTrendAnalyses,
  getTrendAnalyses,
  delTrendAnalyses,
  addTrendAnalyses,
  updateTrendAnalyses,
  exportTrendAnalyses,
} from "@/api/burnPower/trendAnalyses";

export default {
  name: "TrendAnalyses",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 趋势分析信息表格数据
      trendAnalysesList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        pageView: null,
        numberVisitors: null,
        numberIp: null,
        heartRate: null,
        averageAccessTime: null,
        averageAccessPage: null,
      },
      // 时间
      Time: "",
      // 表单参数
      form: {},

      //表单验证
      rules: {
        pageView: [{ required: true, message: "浏览量为空", trigger: "blur" }],
        numbersView: [
          { required: true, message: "浏览量为空", trigger: "blur" },
        ],
        numberIp: [{ required: true, message: "ip数为空", trigger: "blur" }],
        heartRate: [{ required: true, message: "跳率为空", trigger: "blur" }],
        averageAccessTime: [
          { required: true, message: "平均访问时长为空", trigger: "blur" },
        ],
        averageAccessPage: [
          { required: true, message: "平均访问页数为空", trigger: "blur" },
        ],
        createTime: [
          { required: true, message: "问题发布时间不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询趋势分析信息列表 */
    getList() {
      this.loading = true;
      listTrendAnalyses(this.queryParams).then((response) => {
        console.log("趋势数据", response.rows);
        this.trendAnalysesList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },

    // 表单重置
    reset() {
      this.form = {
        id: null,
        pageView: null,
        numberVisitors: null,
        numberIp: null,
        heartRate: null,
        averageAccessTime: null,
        averageAccessPage: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        beginTime: null,
        endTime: null,
      };
      this.Time = null;
      this.queryParams = {
        pageSize: 10,
        newVisitorsPercentage: null,
        numbersView: null,
        numbersVisitors: null,
        heartRate: null,
        averageAccessTime: null,
        averageAccessPage: null,
        beginTime: null,
        endTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      if (this.Time != null) {
        console.log("进入");
        this.queryParams.beginTime = this.Time[0];
        this.queryParams.endTime = this.Time[1];
      }
      // this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.reset();
      // this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加趋势分析信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getTrendAnalyses(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改趋势分析信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateTrendAnalyses(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTrendAnalyses(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm(
        '是否确认删除趋势分析信息编号为"' + ids + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delTrendAnalyses(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有趋势分析信息数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.exportLoading = true;
          return exportTrendAnalyses(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
          this.exportLoading = false;
        })
        .catch(() => {});
    },
  },
};
</script>
<style lang="scss" scoped>
::v-deep .el-form-item__label {
  font-size: 17px;
}
::v-deep .mb8 .el-button {
  font-size: 16px;
}
::v-deep .el-form .el-button {
  font-size: 16px;
}

::v-deep .el-form .el-form-item {
  .el-form-item__error {
    width: 120px;
  }
  .el-form-item__content {
    width: 220px;
  }
}

::v-deep .el-dialog .el-form-item__label {
  width: 30px;
}

//去除input number样式
::v-deep input::-webkit-outer-spin-button,
::v-deep input::-webkit-inner-spin-button {
  -webkit-appearance: none !important;
}
::v-deep input[type="number"] {
  -moz-appearance: textfield !important;
}
::v-deep .el-dialog .el-input__inner {
  width: 300px;
  line-height: 1px !important;
}

.element.style {
  width: 100px;
  min-width: 100px;
}

.app-container {
  background: rgb(254, 254, 254);
}

.pagination_style {
  height: 70px;
  margin-right: 40px;
}

.btn {
  .el-button {
    margin-left: 10px;
    margin-right: 5px;
  }
}
</style>
